package com.example.love_margin.Controller;


import com.example.love_margin.Entity.Search;
import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Service.SearchService;
import com.example.love_margin.Untils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(value = "寻宠/寻主",tags = "管理（寻宠/寻主）")
@RestController
@RequestMapping(value = "/search",name = "寻宠/寻主")
@CrossOrigin
public class SearchController {



    @Autowired
    SearchService searchService;

    //发布(寻宠/寻主)
    @ApiOperation(value = "发布(寻宠/寻主)",notes = "发布(寻宠/寻主)")
    @PostMapping(value = "/addSearchPet")
    public Object addSearchPet(@RequestBody Search search){
        return searchService.addSearchPet(search);
    }

    //删除(寻宠/寻主)
    @ApiOperation(value = "删除(寻宠/寻主)",notes = "删除(寻宠/寻主)")
    @PostMapping(value = "/deleteSearchPet")
    public Object deleteSearchPet(@RequestParam int search_id){
        return searchService.deleteSearchPet(search_id);
    }

    /*
批量删除
*/
    @ApiOperation(value = "删除多个(寻宠/寻主)", notes = "删除多个(寻宠/寻主)，需要数组id")
    @PostMapping(value = "/deleteSearchPets")
    public Object deleteSearchPets(@RequestBody Integer[] search_ids) {
        System.out.println(Arrays.toString(search_ids));
        return searchService.deleteSearchPets(search_ids);
    }


    //修改我的(寻宠/寻主)信息
    @ApiOperation(value = "修改我的(寻宠/寻主)信息",notes = "修改我的(寻宠/寻主)信息")
    @PostMapping(value = "/updateMySearchPet")
    public Object updateMySearchPet(@RequestBody Search search){
        return searchService.updateSearchPet(search);
    }


    //查看单个(寻宠/寻主)信息
    @ApiOperation(value = "查看单个(寻宠/寻主)信息",notes = "查看单个(寻宠/寻主)信息")
    @GetMapping(value = "/selectOneSearchPet")
    public Object selectOneSearchPet(@RequestParam int search_id){
        return searchService.selectOneSearchPet(search_id);
    }


    //查看我的(寻宠/寻主)列表
    @ApiOperation(value = "查看我的(寻宠/寻主)列表",notes = "查看我的(寻宠/寻主)列表")
    @GetMapping(value = "/selectMySearchPet")
    public Object selectMySearchPet(@RequestParam String search_pet_class,
                                    @RequestParam int user_id){
        return searchService.selectSearchPetPage(search_pet_class,user_id);
    }



    //查看(寻宠/寻主)列表
    @ApiOperation(value = "查看(寻宠/寻主)列表",notes = "种类，品种，关键词")
    @GetMapping
            (value = "/selectSearchPet")
    public Object selectSearchPet(@RequestParam(required = false) String search_pet_class,
                                  @RequestParam(required = false) String search_pet_category,
                                  @RequestParam(required = false) String search_pet_varieties,
                                  @RequestParam(required = false) String key,
                                  @RequestParam(required = false) Integer pageNum,
                                  @RequestParam(required = false) Integer pageSize){
        List<Search> search = searchService.selectSearchMany(search_pet_class,search_pet_category, search_pet_varieties,key,pageNum,pageSize);
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<Search> pageInfo = new PageInfo<>(search);
        return ResultCode.SUCCESS.result(pageInfo);
    }


    //通过我的(寻宠/寻主)
    @ApiOperation(value = "通过我的(寻宠/寻主)",notes = "通过我的(寻宠/寻主)")
    @PostMapping(value = "/passMySearch")
    public Object passMySearch(@RequestParam int search_id){
        return searchService.passMySearch(search_id);
    }

}
